package cn.aylog.accountdemo.configs;

import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.Properties;

@Configuration
public class JdbcConfig {

    private static String driverClass;
    private static String url;
    private static String username;
    private static String password;

    static {
        Properties props = new Properties();
        try {
            // 获取配置文件路径
            String configPath = System.getProperty("user.home")+ File.separator +"mysql.properties";
            // 加载配置文件
            props.load(new FileReader(configPath));
            driverClass = props.getProperty("driverClass");
            url = props.getProperty("url");
            username = props.getProperty("username");
            password = props.getProperty("password");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Bean
    public DataSource getDataSource() {
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setDriverClassName(driverClass);
        dataSource.setUrl(url);
        dataSource.setUsername(username);
        dataSource.setPassword(password);
        return dataSource;
    }
}
